package cn.sciento.core.jackson.sensitive;

import cn.sciento.core.jackson.cache.SensitiveCache;
import cn.sciento.core.util.SensitiveUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.util.StringUtils;

public class SensitiveHelper {
    private static ThreadLocal<Boolean> sensitiveSwitch = new ThreadLocal<>();

    public static void open() {
        sensitiveSwitch.set(Boolean.TRUE);
    }

    public static void close() {
        sensitiveSwitch.set(Boolean.FALSE);
    }

    public static boolean isOpen() {
        return BooleanUtils.isTrue(sensitiveSwitch.get());
    }

    public static void remove() {
        sensitiveSwitch.remove();
    }

    public static boolean hasSensitive(String value, Class<?> entityClass, String fieldName) {
        return (StringUtils.hasText(value) && !SensitiveUtils.generateCipherText(SensitiveCache.get(entityClass, fieldName), value).equals(value));
    }
}
